package com.book.dao;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import com.book.entity.Order;
import com.book.util.OrderUtils;

@Repository	
public class OrderDao {
	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	public int saveOrder(){
		
		Order order = new Order(OrderUtils.getOrderNumber(), new Date());
		
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(order);
		
		String sql = "insert into t_order values (S_ORDER_ID.nextval,:orderNumber,:creatTime)";
		
		jdbcTemplate.update(sql, paramSource, generatedKeyHolder, new String[]{"id"});
		
		Number number = generatedKeyHolder.getKey();
		
		return number.intValue();	
	}
}
